<template>
  <div>
    <el-form>
      <el-form-item label="sku名称">
        <el-input placeholder="SKU名称" v-model="params.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" v-model="params.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(克)">
        <el-input placeholder="重量(克)" v-model="params.weight"></el-input>
      </el-form-item>
      <el-form-item label="sku描述">
        <el-input
          type="textarea"
          placeholder="SKU描述"
          v-model="params.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" style="width: 100%">
          <el-form-item
            v-for="(item, index) in attrArr"
            :label="item.attrName"
            :key="item.id"
          >
            <el-select v-model="item.attrIdAndValueId" style="width: 150px">
              <el-option
                :value="`${item.id}:${attrValue.id}`"
                v-for="(attrValue, index) in item.attrValueList"
                :label="attrValue.valueName"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" style="width: 100%">
          <el-form-item
            v-for="(item, index) in saleArr"
            :label="item.saleAttrName"
            :key="item.id"
          >
            <el-select
              v-model="item.saleIdAndValueId"
              placeholder="请选择"
              style="width: 150px"
            >
              <el-option
                :value="`${item.id}:${saleValue.id}`"
                v-for="saleValue in item.spuSaleAttrValueList"
                :label="saleValue.saleAttrValueName"
                :key="saleValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片名称">
        <el-table border :data="imgArr" ref="table">
          <el-table-column align="center" type="selection"></el-table-column>
          <el-table-column label="图片">
            <template #="{ row, $index }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button type="primary" @click="handler(row)">
                设置默认
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="save">保存</el-button>
        <el-button type="primary" size="default" @click="cancel">
          取消
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
// 引入获取属性数据列表的方法
import { reqAttr } from '../../../api/product/attr/index'
// 引入获取销售属性数据列表的方法
import {
  reqSpuSaleAttr,
  reqSpuImg,
  reqSaveSkuInfo,
} from '../../../api/product/spu/index'
import { ref, reactive } from 'vue'
import type { SkuData } from '../../../api/product/spu/type'
import { ElMessage } from 'element-plus'

let $emit = defineEmits(['changeScene'])

// 平台属性
let attrArr = ref<any>()
// SPU销售属性
let saleArr = ref<any>()
// SPU图片属性
let imgArr = ref<any>()
//获取table组件实例
let table = ref<any>()

// SKU表单数据对象
let params = reactive<SkuData>({
  //父组件传递过来的数据
  id: null, // SKU的ID
  category3Id: null, //三级分类的ID
  spuID: null, //已有的SPU的ID
  tmId: null, //SPU品牌的ID
  skuName: '', //sku名字
  price: null, //sku价格
  weight: null, //sku重量
  skuDesc: '', //sku的描述
  isSale: null,
  skuAttrValueList: [
    //平台属性的收集
  ],
  skuSaleAttrValueList: [
    //销售属性
  ],
  skuDefaultImg: '', //sku图片地址
})

// 子组件向父组件暴露的初始化子组件表单数据的方法
const initSkuData = async (
  categoryC1: number,
  categoryC2: number,
  row: any,
) => {
  // 获取当前三级分类下对应的品牌属性
  let result = await reqAttr(categoryC1, categoryC2, row.category3Id)
  attrArr.value = result.data

  // 获取当前分类下的销售属性
  let result1 = await reqSpuSaleAttr(row.id)
  saleArr.value = result1.data

  // 获取已有SPU下所有的图片列表
  let result2 = await reqSpuImg(row.id)
  imgArr.value = result2.data

  // 收集数据
  params.category3Id = row.category3Id
  params.spuID = row.id
  params.tmId = row.tmId
}

// 取消按钮方法
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' })
}

//设置默认图片的方法回调
const handler = (row: any) => {
  //点击的时候,全部图片的的复选框不勾选
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  //选中的图片才勾选
  table.value.toggleRowSelection(row, true)
  //收集图片地址
  params.skuDefaultImg = row.imgUrl
}

//保存按钮的方法
const save = async () => {
  //整理参数
  //平台属性
  params.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    return prev
  }, [])
  //销售属性
  params.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId,
        saleAttrValueId,
      })
    }
    return prev
  }, [])
  //添加SKU的请求
  let result: any = await reqSaveSkuInfo(params)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功',
    })
    //通知父组件切换场景为零
    $emit('changeScene', { flag: 0, params: '' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
}
defineExpose({ initSkuData })
</script>

<style lang="scss" scoped></style>
